{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding:utf-8\n",
    "import urllib, sys\n",
    "import ssl, json\n",
    "\n",
    "AK = \"xPkXLyWbKYvwj3yIHH4bREPX\"\n",
    "SK = \"NBdeA8QHGojRt93mqkkC5dHZ1QV053HR\"\n",
    "\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials'\\\n",
    "       '&client_id=%s'\\\n",
    "       '&client_secret=%s' % (AK, SK)\n",
    "\n",
    "def GetToken():\n",
    "    request = urllib.request.Request(host)\n",
    "    request.add_header('Content-Type', 'application/json; charset=UTF-8')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    if (content):\n",
    "        js = json.loads(content)\n",
    "        # return js['refresh_token']\n",
    "        return js['access_token']\n",
    "    return None\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "token = GetToken()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24岁的小姐姐, 颜值 53.47 分\n",
      "{'face_token': 'e7e36e66f4f3d77dbeb8e420f78a4e09', 'location': {'left': 366.9010315, 'top': 338.0498047, 'width': 95, 'height': 99, 'rotation': -8}, 'face_probability': 1, 'angle': {'yaw': -11.88787174, 'pitch': -12.93472004, 'roll': -10.1117506}, 'age': 24, 'beauty': 53.47278976, 'expression': {'type': 'none', 'probability': 0.9998955727}, 'face_shape': {'type': 'round', 'probability': 0.3698224127}, 'gender': {'type': 'female', 'probability': 0.9999614954}, 'glasses': {'type': 'none', 'probability': 0.999999404}, 'race': {'type': 'yellow', 'probability': 0.9752483964}, 'face_type': {'type': 'human', 'probability': 0.9802666306}}\n",
      "26岁的小哥哥, 颜值 48.38 分\n",
      "{'face_token': '9005f50e22fbc7222e978558dc414e58', 'location': {'left': 149.0172272, 'top': 182.9704895, 'width': 95, 'height': 87, 'rotation': -4}, 'face_probability': 1, 'angle': {'yaw': -21.4942646, 'pitch': -0.104852058, 'roll': -8.236881256}, 'age': 26, 'beauty': 48.37926483, 'expression': {'type': 'none', 'probability': 0.9999856949}, 'face_shape': {'type': 'round', 'probability': 0.6585451365}, 'gender': {'type': 'male', 'probability': 0.9999217987}, 'glasses': {'type': 'none', 'probability': 0.9999808073}, 'race': {'type': 'yellow', 'probability': 0.9999163151}, 'face_type': {'type': 'human', 'probability': 0.9422109127}}\n",
      "24岁的小姐姐, 颜值 58.40 分\n",
      "{'face_token': 'ecbaf5f8fd8cd68a2a3e9ff8028f3cc9', 'location': {'left': 307.6585693, 'top': 191.2546692, 'width': 88, 'height': 86, 'rotation': -7}, 'face_probability': 1, 'angle': {'yaw': -17.00756454, 'pitch': -2.246499062, 'roll': -9.978621483}, 'age': 24, 'beauty': 58.3964386, 'expression': {'type': 'none', 'probability': 0.9998424053}, 'face_shape': {'type': 'heart', 'probability': 0.4708323479}, 'gender': {'type': 'female', 'probability': 0.999125421}, 'glasses': {'type': 'none', 'probability': 0.9999984503}, 'race': {'type': 'yellow', 'probability': 0.9999446869}, 'face_type': {'type': 'human', 'probability': 0.9972125888}}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import base64\n",
    "import urllib, json\n",
    "'''\n",
    "人脸探测\n",
    "'''\n",
    " \n",
    "url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    " \n",
    "def FaceDetect(pic, token):\n",
    "    # 二进制方式打开图片文件\n",
    "    f = open(pic, 'rb')\n",
    "    img = base64.b64encode(f.read())\n",
    "    f.close()\n",
    "                        #  年龄  颜值    表情      脸型      性别   眼镜   人种 真实/卡通   landmark(4个关键点位置 72个特征点位置),quality(人脸质量信息)\n",
    "    params = {\"face_field\":\"age,beauty,expression,faceshape,gender,glasses,race,face_type\",\"image\":img,\"image_type\":\"BASE64\",\"max_face_num\":5}\n",
    "    params = bytes(urllib.parse.urlencode(params), encoding='utf-8')\n",
    " \n",
    "    request_url = url + \"?access_token=\" + token\n",
    "    request = urllib.request.Request(url=request_url, data=params)\n",
    "    request.add_header('Content-Type', 'application/x-www-form-urlencoded')\n",
    "    response = urllib.request.urlopen(request)\n",
    "    content = response.read()\n",
    "    if content:\n",
    "        js = json.loads(content)\n",
    "        result = js['result']\n",
    "        face_list = result['face_list']\n",
    "        for item in face_list:\n",
    "            if 'female'==item['gender']['type']:\n",
    "                sx = '小姐姐'\n",
    "            else:\n",
    "                sx = '小哥哥'\n",
    "            print('%d岁的%s, 颜值 %2.2f 分' % (item['age'], sx, item['beauty']))\n",
    "            print(item)\n",
    "        # print js['result'][0]\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    FaceDetect(r\"D:\\Project\\Python3\\spider\\Scripts\\user\\img/3.png\", token)         # 图片名：pic 文件夹里的 1.jpg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
